package com.sjft.cloud.commons.quartz;

import com.sjft.cloud.commons.quartz.event.QuartzProcessEvent;
import com.sjft.cloud.commons.core.utils.SpringContextUtils;
import org.quartz.JobDetail;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractQuartzJobBean extends QuartzJobBean {


    public abstract void process();


    @Override
    protected void executeInternal(org.quartz.JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        Map<String, Object> log = new HashMap<>();
        log.put("jobName", jobDetail.getDescription());
        log.put("fireTime", jobExecutionContext.getScheduledFireTime());
        log.put("preFireTime", jobExecutionContext.getPreviousFireTime());
        log.put("nextFireTime", jobExecutionContext.getNextFireTime());
        try {
            process();
            log.put("processState", 1);
        } catch (Exception e) {
            log.put("processState", 0);
        }
        SpringContextUtils.publishEvent(new QuartzProcessEvent(log));
    }
}
